package com.voxel.sense.infomsg.utils;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import java.io.File;

import cyy.example.com.facotry.Factory;

/**
 * Created by ChenYiYao on 2017/12/19.
 */

public class FileProvider7 {
    public static Uri getUriForFile(File file) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = getUriForFile24(file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    public static Uri getUriForFile24(File file) {
        Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(Factory.app(),
                Factory.app().getPackageName() + ".android7.fileprovider",
                file);
        return fileUri;
    }


    /**
     *
     * @param intent
     * @param type 文件类型
     * @param file 文件路径
     * @param writeAble 是否写
     */
    public static void setIntentDataAndType(Intent intent,
                                            String type,
                                            File file,
                                            boolean writeAble) {
        if (Build.VERSION.SDK_INT >= 24) {
            intent.setDataAndType(getUriForFile(file), type);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (writeAble) {
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        } else {
            intent.setDataAndType(Uri.fromFile(file), type);
        }
    }

}
